package cn.bluethink.eguan.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.bluethink.eguan.core.security.Token;
import cn.bluethink.eguan.core.service.EgStaffService;
import cn.bluethink.eguan.core.service.EgTavernService;
import cn.bluethink.eguan.core.service.EgUserService;
import cn.bluethink.eguan.filter.NameFilter;
import cn.bluethink.eguan.model.core.EgFaction;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgObject;
import cn.bluethink.eguan.model.core.EgPurview;
import cn.bluethink.eguan.model.core.EgTavern;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.result.PageInfo;
import cn.bluethink.eguan.result.ResponseMessage;
import cn.bluethink.eguan.result.ResponseResult;

/**
 * 驿馆伙计控制器
 * 
 * @title 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.1.0
 * @describe
 * @author liuxn
 * @date 2018年10月16日
 * @note 修订历史： （1）liuxn于2018年10月16日构建初始版本0.1 
 */
@CrossOrigin
@RestController
@RequestMapping("/eguan/v1.0/staff")
public class EgStaffController {
    
	@Autowired
	private EgStaffService staffService;

    @Autowired
    private EgTavernService tavernService;

    @Autowired
    private EgUserService userService;
	
	/**
	 * 根据条件查询驿馆伙计列表
	 * 
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	@Token
	@PostMapping("/query")
	public ResponseResult<PageInfo<EgUser>> query(@RequestBody NameFilter filter) throws Exception {
		return new ResponseResult<>(ResponseMessage.OK, staffService.query(filter, true));
	}
	
	/**
	 * 批量添加驿馆伙计
	 * 
	 * @param egid 驿馆Id
	 * @param uids 伙计Id列表
	 * @return 返回驿馆Id
	 * @throws Exception
	 */
	@Token(purviews = {EgPurview.STAFF_MG})
	@PostMapping("/add")
	public ResponseResult<Long> add(@RequestParam Long egid, @RequestParam List<Long> uids) throws Exception {
		if(uids.size() == 0) return new ResponseResult<>(ResponseMessage.PARAMETER_ERROR.getStatus(), "uids不能为空！");
        EgTavern tavern = tavernService.getById(egid);
        if(tavern == null) {
        	return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "不存在该驿馆，请重试！");
        }
		if(staffService.batchAdd(tavern, uids)) {
			return new ResponseResult<>(ResponseMessage.OK, egid);
		}
		return new ResponseResult<>(ResponseMessage.CREATE_FAIL.getStatus(), "调整驿馆伙计失败！");
	}
	
	/**
	 * 根据驿馆Id和用户ID移除当前驿馆中的伙计
	 * @param egid 驿馆Id
	 * @param uid 用户Id
	 * @return 返回驿馆Id
	 * @throws Exception
	 */
	@Token(purviews = {EgPurview.STAFF_MG, EgPurview.OBJECT_CHECK})
	@PostMapping("/remove")
	public ResponseResult<Long> remove(@RequestParam Long egid, @RequestParam Long uid) throws Exception {
        EgUser user = userService.detail(uid);
        EgTavern tavern = tavernService.getById(egid);
        if(user == null) {
        	return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "系统中不存在改用户，请联系系统管理员！");
        }
        if(tavern == null) {
        	return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "不存在该驿馆，请重试！");
        }
        // 删除驿馆伙计
		if(staffService.remove(egid, uid).equals(0L)) 
			return new ResponseResult<>(ResponseMessage.DELETE_FAIL);
        // 判断当前用户所在位置
        EgObject pos = user.getPos();
        // 如果用户在当前驿馆，调整其所在位置
        if(pos != null && pos.getType().getOtid() == EgOType.TAVERN && pos.getId().equals(egid)) {
        	EgFaction faction = tavern.getFaction();
        	Integer otid = EgOType.JIANGHU;
        	Long fid = 0L;
        	// 如果驿馆隶属于门派，将用户位置改成其隶属门派
        	if(faction != null && faction.getId() > 0)  {
        		otid = EgOType.FACTION;
        		fid = faction.getId();
        	}
        	// 否则，用户位置为江湖
            if (userService.updatePos(uid, otid, fid)) return new ResponseResult<>(ResponseMessage.OK, egid);
        	return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND);
        }
        // 不在当前驿馆，不做多余操作，删除成功
        return new ResponseResult<>(ResponseMessage.OK, egid);
	}
	
	/**
	 * 设置驿馆掌柜
	 * @param egid 驿馆Id
	 * @param uid 用户Id
	 * @return 返回用户信息，包括角色
	 * @throws Exception
	 */
	@Token(purviews = {EgPurview.APPOINT_SHOPKEEPER})
	@PostMapping("/setshopkeeper")
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
	public ResponseResult<EgUser> setshopkeeper(@RequestParam Long egid, @RequestParam Long uid) throws Exception {
		// 1、移除原掌柜在staff表中的掌柜角色
		if(!staffService.removeShopKeeperRole(egid)) return new ResponseResult<>(ResponseMessage.UPDATE_FAIL);
		// 2、根据egid和uid更新当前用户在staff表中的角色和权限
		EgUser result = staffService.setshopkeeper(egid, uid, false);
		if(result == null) return new ResponseResult<>(ResponseMessage.UPDATE_FAIL);
		return new ResponseResult<>(ResponseMessage.OK, result);
	}
}
